package com.kerrykidz.sms.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kerrykidz.sms.dao.mapper.SmsOutBoxAndBatchCondEntityMapper;
import com.kerrykidz.sms.dao.model.SmsOutBoxEntity;
import com.kerrykidz.sms.service.ISmsSendService;
import com.kerrykidz.sms.util.BatchCondUtil;
import com.kerrykidz.sms.util.SmsSendVO;
import com.rdp.framework.exception.RDPException;

@Service
public class SmsSendServiceImpl implements ISmsSendService {


	@Autowired
	private SmsOutBoxAndBatchCondEntityMapper smsOutBoxAndBatchCondMapper;
	
	@Override
	public int sendSMSAndUpdateOutBox(SmsSendVO sendVo) throws RDPException {
		if(BatchCondUtil.isBlank(sendVo.getSmsBatchId(),sendVo.getSmsMob(),sendVo.getSmsText())){
			/**组合发送条件*/
			StringBuffer sb = new StringBuffer();
			sb.append(sendVo.getUrl()).append("?Uid=").append(sendVo.getuId()).append("&Key=").append(sendVo.getKey());
			sb.append("&smsMob=").append(sendVo.getSmsMob()).append("&smsText=").append(sendVo.getSmsText());
			
			int count = sms(sb.toString());
			String status = SmsOutBoxEntity.UNSEND;
			
			/**更新发送结果*/
			if(count > 0 ){
				status = SmsOutBoxEntity.SEND;
			}else{
				status = SmsOutBoxEntity.SEND_FAIL;
			}
			/**确保短信正确性*/
			List<String> mobList = BatchCondUtil.split(sendVo.getSmsMob(), ",");
			if(mobList.size() > 0){
				smsOutBoxAndBatchCondMapper.updateSmsOutBox(sendVo.getSmsBatchId(),status, null, mobList);
			}
			
		}else{
			return SmsSendVO.SEND_PARAM_ERROR;
		}
		
		return 0;
	}

	@Override
	public int sendSMS(SmsSendVO sendVo) throws RDPException {
		if(BatchCondUtil.isBlank(sendVo.getSmsMob(),sendVo.getSmsText())){
			StringBuffer sb = new StringBuffer();
			sb.append(sendVo.getUrl()).append("?Uid=").append(sendVo.getuId()).append("&Key=").append(sendVo.getKey());
			sb.append("&smsMob=").append(sendVo.getSmsMob()).append("&smsText=").append(sendVo.getSmsText());
			return sms(sb.toString());
		}else{
			return SmsSendVO.SEND_PARAM_ERROR;
		}
		
		
	}
	
	/***
	 * 发送短信消息
	 * @param url
	 * @return
	 * @date: 2014年10月8日
	 */
	private int sms(String url){
		System.out.println("发送短信内容:" + url);
		return 0;
	}

}
